#include "../../inc/skyFile.h"
#ifdef _WIN_
#include "../../inc/skyString.h"
#include <Windows.h>
#include <strsafe.h>
namespace Sky
{

    Bool File::open( String* name,OpenMode flag )
    {
		_name = name;
        hFile = CreateFile(_name->toCharString(),GENERIC_READ,0,0,OPEN_EXISTING,0,0);
        return hFile != INVALID_HANDLE_VALUE ? vTrue : vFalse;
    }

    void File::close()
    {
        _bOpen = vFalse;
        CloseHandle(hFile);
        hFile = vNull;
    }

    Sky::Bool File::seekToEnd()
    {
        return vFalse;
    }

    Sky::Bool File::seekToBegin()
    {
        return vFalse;
    }

    Sky::u32 File::getLength() const
    {
		BY_HANDLE_FILE_INFORMATION FileInfo; 

		if(!GetFileInformationByHandle(hFile,&FileInfo))return -1;

		return FileInfo.nFileSizeLow;
    }

    Sky::Bool File::seek( i32 lOff, u32 nFrom )
    {
        return 0;
    }

    Sky::i32 File::write( const u8* lpBuf, u32 nCount )
    {
        DWORD written;
        return vTrue == WriteFile(hFile,lpBuf,nCount,(LPDWORD)&written,vNull) ? written : -1;
    }

    Sky::i32 File::read( u8* lpBuf, u32 nCount )
    {
        DWORD read;
        return vTrue == ReadFile(hFile,lpBuf,nCount,(LPDWORD)&read,vNull) ? read : -1;
    }

    OSFile::OSFile():
    hFile(vNull)
    {

    }

    i32 OSFile::readLine( u8* buff,u32 buffLen )
    {
	    //u8 ch;
	   // DWORD r;
	    //for(;;)
	    //{
		   // if(vFalse == ReadFile(hFile,&ch,sizeof(ch),&r,vNull))
			  //  return -1;
		   // if(Sky::NHelper::Helper::isVisible(ch))
			  //  break;
	    //}
    	//
	    //u32 n = 0;
	    //for(;;)
	    //{
		   // if(n == buffLen)
			  //  return -1;
		   // if(!Sky::NHelper::Helper::isVisible(ch))
		   // {
			  //  *read = n;
			  //  return read;
		   // }
		   // buff[n] = ch;
		   // n++;
		   // if(vFalse == ReadFile(hFile,&ch,sizeof(ch),&r,vNull))
			  //  return -1;
	    //}
	    return -1;
    }

    OSDirectory::OSDirectory():
    hListFile(INVALID_HANDLE_VALUE)
    {
    
    }

    const StringPtr OSDirectory::listFileInDir( const StringPtr& dir,const StringPtr& fileMask,Bool isFirst /*= vTrue*/,StringPtr& out )
    {
		StringPtr& dirPath = const_cast<StringPtr&>(dir);
    	if(isFirst)
    	{
    		Char szFiles[MAX_PATH];
    		if( GetFileAttributes( dirPath->toCharString() ) == 0xFFFFFFFF )
    		{
    			//StringCchCopy(dirRet,dirRetLen,strSearchPath);
    			return out;
    		}
    		StringCchPrintf( szFiles, sizeof(szFiles), _XT("%s\\%s"),dirPath->toCharString(),const_cast<StringPtr&>(fileMask)->toCharString() ); 
    
    		hListFile = FindFirstFile(szFiles,&FindFileData);
    		if(hListFile == INVALID_HANDLE_VALUE)
    			return out;
            out->copyFrom(FindFileData.cFileName);
    	}
    	else
    	{
    		if(FindNextFile(hListFile, &FindFileData))
            {
    			out->copyFrom(FindFileData.cFileName);
            }
    	}
    	return out;	
    }
}

#endif


